package main.http;

/**
 * Created by hdt3213 on 17/3/27.
 */

import main.http.HttpCookie;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.InputStream;

public class HttpResponse {

	static final String LineFeed = "\n";

	static final HashMap<Integer, String> STATUS = new HashMap<Integer, String>();

	static {
		STATUS.put(100, "100 Continue");
		STATUS.put(101, "101 Switching Protocols");
		STATUS.put(200, "200 OK");
		STATUS.put(201, "201 Created");
		STATUS.put(204, "204 No Content");
		STATUS.put(206, "206 Partial Content");
		STATUS.put(300, "300 Multiple Choices");
		STATUS.put(301, "301 Moved Permanently");
		STATUS.put(302, "302 Found");
		STATUS.put(303, "303 See Other");
		STATUS.put(304, "304 Not Modified");
		STATUS.put(307, "307 Temporary Redirect");
		STATUS.put(400, "400 Bad Request");
		STATUS.put(401, "401 Unauthorized");
		STATUS.put(403, "403 Forbidden");
		STATUS.put(404, "404 Not Found");
		STATUS.put(405, "405 Method Not Allowed");
		STATUS.put(406, "406 Not Acceptable");
		STATUS.put(408, "408 Request Timeout");
		STATUS.put(410, "410 Gone");
		STATUS.put(413, "413 Request Entity Too Large");
		STATUS.put(414, "414 Request URI Too Long");
		STATUS.put(415, "415 Unsupported Media Type");
		STATUS.put(416, "416 Requested Range Not Satisfiable");
		STATUS.put(417, "417 Expectation Failed");
		STATUS.put(500, "500 Internal Server Error");
		STATUS.put(501, "501 Method Not Implemented");
		STATUS.put(503, "503 Service Unavailable");
		STATUS.put(506, "506 Variant Also Negotiates");
	}

	boolean built = false;
	int status;
	String protocol = "";
	HashMap<String, String> headers = new HashMap<String, String>();
	HashMap<String, HttpCookie> cookies = new HashMap<String, HttpCookie>();
	String body = "";
	String txt = "";

	public HttpResponse() {
		protocol = "HTTP/1.1";
		status = 200;
	}

	public HttpResponse(String body) {
		protocol = "HTTP/1.1";
		status = 200;

		this.body = body;
	}

	public void build() {
		headers.put("Content-Type", "text/html");
		headers.put("Content-Length", String.valueOf(body.length()));
		// build protocol
		txt = protocol + " " + STATUS.get(status) + LineFeed;

		// add cookie
		addCookies();

		// build header
		for (HashMap.Entry<String, String> entry : headers.entrySet()) {
			txt += entry.getKey() + ":" + entry.getValue() + LineFeed;
		}
		// build cookie

		// build body
		txt += "\r\n";
		txt += body;
		built = true;
	}

	public void loadFile(String filename) {
		try {
			InputStream is = new FileInputStream(filename);
			String line;
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			while ((line = reader.readLine()) != null) {
				body += line;
			}
			reader.close();
			is.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void addCookies() {
		for (HashMap.Entry entry: cookies.entrySet()) {
			HttpCookie cookie = (HttpCookie) entry.getValue();
			headers.put("Set-Cookie", cookie.build());
		}
	}

	public String getTxt() {
		if (!built) {
			build();
		}
		return txt;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getBody() {
		return body;
	}

	public void setHeader(String key, String val) {
		headers.put(key, val);
	}

	public String getHeader(String key) {
		return headers.get(key);
	}

	public void setCookie(String key, HttpCookie val) {
		cookies.put(key, val);
	}

	public HttpCookie getCookie(String key) {
		return cookies.get(key);
	}

	public void setCookie(String key, String val) {
		cookies.put(key, new HttpCookie(key, val));
	}

	public static void main(String[] args) {
		HttpResponse response = new HttpResponse();
		response.loadFile("resource/index.html");
		System.out.println(response.getTxt());
	}

}
